package com.his.struts.action;

import java.sql.ResultSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.his.struts.dao.note.NoteDao;
import com.his.struts.dao.note.NoteImp;
import com.his.struts.dao.users.UsersDao;
import com.his.struts.dao.users.UsersImp;
import com.his.struts.form.NoteForm;
import com.his.struts.form.UserForm;
import com.his.struts.util.PageUtil;

/**
 * 日志功能类
 * 
 * @author liuyan
 * 
 */
public class NoteAction extends DispatchAction {
	NoteDao dao = new NoteImp();

	/**
	 * Method 取所有日记
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward allNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		HttpSession session = request.getSession();
		UserForm userinfo = (UserForm) session.getAttribute("userinfo");
		if(userinfo == null){
			return mapping.findForward("login");
		}
		int uid = userinfo.getUid();
		String sql = "notebook where u_id = " + uid + " order by u_id desc";
		// 得到分页数据
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		if (rs != null) {
			// 封装
			List<NoteForm> shownote = dao.allNote(rs);
			// 数据放到request中
			request.setAttribute("shownote", shownote);
		}

		int i = dao.allNoteNum(uid);
		request.setAttribute("allNum", i);
		forword = mapping.findForward("successall");

		return forword;
	}
	/**
	 * 查看日记
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward showNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		String nid = request.getParameter("noteid");
		int noteid = Integer.parseInt(nid);
		// 掉用NoteDao类showNote()取日记
		NoteForm nf = dao.showNote(noteid);
		if (nf != null) {
			request.setAttribute("mytestnote", nf);
			forword = mapping.findForward("successshow");
		} else {
			forword = mapping.findForward("fail");
		}
		return forword;
	}
	
	/**
	 * 删除日记
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward delNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		String nid = request.getParameter("noteid");
		int noteid = Integer.parseInt(nid);
		// 掉用NoteDao类showNote()取日记
		//调用NoteDao类里面的showNote（）方法删除一个日记
		boolean nf = dao.delNote(noteid);
		if(nf){
			request.setAttribute("message", "删除成功");
			forword = mapping.findForward("successDel");
		}
		return forword;
	}

	/**
	 * 编辑日记
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward editNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		NoteForm nf = (NoteForm) form;
		ActionForward forword = null;
		if(!"".equals(nf.getNb_title())){
			
		// 得到session中的token
		String token = (String)request.getSession().getAttribute("token");
		// 得到网页中的token
		String newToken = request.getParameter("token");
		// 是否重复提交
		if(newToken != null && newToken.equals(token)){
			// 修改基本信息
			return mapping.findForward("successedit");
		}
		// 把最新token设置到session中
		request.getSession().setAttribute("token", newToken);
		
		NoteDao ndao = new NoteImp();
		UsersDao dao = new UsersImp();
		HttpSession session = request.getSession();
		String uname = (String) session.getAttribute("username");
		int uid = dao.getuid(uname);
			
		nf.setU_id(uid);
		// 保存日记
		boolean ok = ndao.editNote(nf);
		if (ok) {
			request.setAttribute("message", "修改成功");
			forword = mapping.findForward("successedit");
		}
		}else{
			request.setAttribute("nullEdit", "请输入标题！！！");
			forword = mapping.findForward("editNull");
		}
		return forword;
	}

	/**
	 * 搜索日记
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward searchNote(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		ActionForward forword = null;
		String id = request.getParameter("did");
		int did = Integer.parseInt(id);
		String sql = "notebook where d_id = " + did + " order by d_id desc";
		// 得到分页数据
		ResultSet rs = PageUtil.getPage(sql, 10, request);
		List<NoteForm> shownote = null;
		if (rs != null) {
			// 封装
			shownote = dao.allNote(rs);
			int i = dao.allNoteNum(did);
			request.setAttribute("shownote", shownote);
			request.setAttribute("allNum", i);
		} 
		return forword = mapping.findForward("successall");
	}

}